<?php

namespace app\admin\controller\commission;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;
use think\Url;

/**
 * @icon fa fa-circle-o
 */
class Commission extends Backend{
    
    public function _initialize()
    {
        parent::_initialize();
    }

    /*查看*/
    public function index(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            $where = array();
            $where['t.category'] = array('eq', 5); //代办功能
            $where['a.is_show'] = array('eq', 1);
            
            $total = Db::name('amoy')
                ->alias('a')
                ->join('cy_type t', 'a.type_id = t.type_id')
                ->where($where)
                ->order('a.amoy_id DESC')
                ->count();
            
            $list = Db::name('amoy')
                ->alias('a')
                ->join('cy_type t', 'a.type_id = t.type_id')
                ->field('amoy_id, title, cover, auto_content, word_source, add_time')
                ->where($where)
                ->order('a.amoy_id DESC')
                ->limit($offset, $limit)
                ->select();
            foreach($list as &$v) {
                //$v['auto_content'] = preg_replace("/<(\/?html.*?)>/si","",$v['auto_content']);
                $v['auto_content'] = mb_substr(strip_tags($v['auto_content']), 0, 80, 'utf-8');
            }
            $result = array("total" => $total, "rows" => $list);
            
            return json($result);
        }
        
        //设置列表工具栏
        $btns = array('refresh', 'add');
        
        $this->view->assign('btns', $btns);
        return $this->view->fetch();
    }
     /**
     * 添加
     */
    public function add(){
        if ($this->request->isPost()) {
            $type_id = Db::name('type')->where(array('category' => 5))->value('type_id');
            $inputData = input();
            $add_data = array(
                'user_id' => 1,
                'type_id' => $type_id,
                'title' => $inputData['ad_title'],
                'cover' => $inputData['picture'],
                'auto_content' => $inputData['auto_content'],
                'word_source' => $inputData['source'],
                'add_time' => time()
            );
            $re = Db::name('amoy')->insert($add_data);
            if($re) {
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        }
        return $this->view->fetch();
    }
    /*编辑*/
    public function edit($ids = NULL){
        $amoy_id = $ids;
        
        $row = Db::name('amoy')->field('amoy_id, title, auto_content, word_source, cover')->where(array('amoy_id' => $amoy_id))->find();
        
        if ($this->request->isPost()) {
            $inputData = input();
            $save_data = array(
                'title' => $inputData['ad_title'],
                'auto_content' => $inputData['auto_content'],
                'cover' => $inputData['picture'],
                'word_source' => $inputData['source'],
                'up_time' => time()
            );
            $re = Db::name('amoy')->where(array('amoy_id' => $amoy_id))->update($save_data);
            if($re) {
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        }
        
        $this->assign('row', $row);
        return $this->view->fetch();
    }
    /*删除*/
    public function del($ids = ""){
        $amoy_id = $ids;
        $status = Db::name('amoy')->where(array("amoy_id" => $amoy_id))->update(array("is_show"=>0));
        if($status){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}